package com.observatory.baapp;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class EventBus {
    private static EventBus instance;
    private List<EventListener> listeners = new ArrayList<>();

    public static EventBus getInstance() {
        if (instance == null) {
            instance = new EventBus();
        }
        return instance;
    }

    public interface EventListener {
        void onEvent(String eventType);
    }

    public void register(EventListener listener) {
        listeners.add(listener);
    }

    public void unregister(EventListener listener) {
        listeners.remove(listener);
    }

    public void post(String eventType) {
        Log.d("EventBus", "发布事件: " + eventType + ", 订阅者数量: " + listeners.size());
        for (EventListener listener : listeners) {
            Log.d("EventBus", "通知订阅者执行事件");
            listener.onEvent(eventType);
        }
    }
}
